package cn.edu.sdjzu.xg.eagle.controller;


import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.factory.ServiceFactory;
import cn.edu.sdjzu.xg.eagle.Service.SchoolService;
import cn.edu.sdjzu.xg.eagle.Service.SchoolServiceInterface;
import cn.edu.sdjzu.xg.eagle.util.ControllerHelper;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Provider;
import java.sql.SQLException;
import java.util.Collection;

@WebServlet("/schoolRead.ctl")
public class SchoolReadCtl extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        JSONObject message = new JSONObject();
        String id_str = req.getParameter("id");
        try{
            Long id_long = Long.parseLong(id_str);
            School school = ServiceFactory.getService(SchoolServiceInterface.class,"SchoolService").find(id_long);
            String school_json = JSON.toJSONString(school, JSONWriter.Feature.ReferenceDetection);
            resp.getWriter().println(school_json);
        } catch (BysjException e) {
            message.put("msg", e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("msg", "数据库操作异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("msg", "网络异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        JSONObject message = new JSONObject();
        try {
            Collection<School> schoolList = SchoolService.getInstance().findAll();
            String schools_json = JSON.toJSONString(schoolList, JSONWriter.Feature.ReferenceDetection);
            resp.getWriter().println(schools_json);
        } catch (BysjException e) {
            message.put("msg", e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("msg", "数据库操作异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("msg", "网络异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        resp.setContentType("text/html;charset=utf-8");
        String req_jsonStr = JSONUtil.getJSON(req);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj==null){
            message_jsonObj.put("message","未收到请求消息");
            resp.getWriter().println(message_jsonObj);
            return;
        }
        try {
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            Collection<School> schools = SchoolService.getInstance().findAll(pagination);
            String schools_json = JSON.toJSONString(schools,JSONWriter.Feature.ReferenceDetection);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("data",schools);
            resp.getWriter().println(message_jsonObj);
        } catch (BysjException e) {
            message_jsonObj.put("msg", e.getMessage());
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();
        } catch (SQLException e) {
            message_jsonObj.put("msg", "数据库操作异常");
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();
        } catch (Exception e) {
            message_jsonObj.put("msg", "网络异常");
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }

}
